// component/rightList/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    refreshList: {
      type: Boolean,
      value: false
    }
  },

  observers:{
    'refreshList': function (refreshList) {
      if (refreshList) {
        this.getRightList()
        // console.log('刷新')
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    loading: false,
    showDeleteModal: false,
    id: '',
    list: []
  },
  created: function () {
    this.getRightList()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    lookHome(e) {
      wx.navigateTo({
        url: '/pages/personalHome/index?openid=' + e.currentTarget.dataset.openid,
      })
    },
    getRightList() {
      this.setData({
        loading: true
      })
      wx.cloud.callFunction({
        name: 'getRightList',
        data: {
          state: 'hasRight',
          page: 1
        }
      }).then(res => {
        // console.log(res.result)
        this.properties.refreshList = false
        this.setData({
          list: res.result,
          loading: false
        })
      })
    },
    cancelRight(){
      wx.showLoading({
        title: '取消中',
        mask: true,
      })
      wx.cloud.callFunction({
        name: 'deleteRight',
        data: {
          id: this.data.id
        }
      }).then(res => {
        if (res.result == 1) {
          this.setData({
            showDeleteModal: false
          })
          this.data.list = this.data.list.filter(item => {
            return item._id != this.data.id
          })
          this.setData({
            id: '',
            list: this.data.list
          })
          wx.showToast({
            title: '已取消',
            icon: 'success',
            duration: 1000
          })
        }
      })
    },
    deleteRight() {
      this.setData({
        showDeleteModal: true
      })
    },
    hideModal() {
      this.setData({
        showDeleteModal: false
      })
    },
    // ListTouch触摸开始
    ListTouchStart(e) {
      this.setData({
        id: e.currentTarget.dataset.id,
        ListTouchStart: e.touches[0].pageX
      })
    },

    // ListTouch计算方向
    ListTouchMove(e) {
      this.setData({
        ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
      })
    },

    // ListTouch计算滚动
    ListTouchEnd(e) {
      if (this.data.ListTouchDirection == 'left') {
        this.setData({
          modalName: e.currentTarget.dataset.target
        })
      } else {
        this.setData({
          modalName: null
        })
      }
      this.setData({
        ListTouchDirection: null
      })
    },
  }
})
